//
//  ScanGoodDetailVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/11.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ScanGoodDetailVC.h"
#import "ScanGoodDetailModel.h"
#import "BillAddPackageGoodModel.h"
#import "ScanGoodDetailInfoCell.h"
#import "ScanGoodDetailListCell.h"

@interface ScanGoodDetailVC ()<UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <ScanGoodDetailModel *>*dataArray;
@property (nonatomic, strong) BillAddPackageGoodModel *model;

@end

@implementation ScanGoodDetailVC

-(NSMutableArray<ScanGoodDetailModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - BottomHeight - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"已扫码货物清单";
    page = 1;
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:ReceiptDeliveryBillAddPackageScanGoodDetail params:@{@"wa_id":self.wa_id,@"g_id":self.g_id,@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model = [BillAddPackageGoodModel mj_objectWithKeyValues:responseObject[@"waybillInfo"]];
            if (self->page == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [ScanGoodDetailModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
            }else {
                NSArray *array = [ScanGoodDetailModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.dataArray addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ScanGoodDetailInfoCell *cell = [ScanGoodDetailInfoCell ScanGoodDetailInfoCellWithTableView:tableView];
        cell.model = self.model;
        return cell;
    }else {
        ScanGoodDetailListCell *cell = [ScanGoodDetailListCell ScanGoodDetailListCellWithTableView:tableView];
        ScanGoodDetailModel *model = self.dataArray[indexPath.row];
        cell.model = model;
        __weak_self;
        cell.jieChuBlock = ^{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定要解除该包装与运单的绑定吗？" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认解除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [weakself requestForJieChu:model.num_sn];
            }];
            UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:sureAction];
            [alert addAction:cancleAction];
            [weakself presentViewController:alert animated:YES completion:nil];
        };
        return cell;
    }
}

- (void)requestForJieChu:(NSString *)num_sn {
    [HttpRequest PostWithApi:ReceiptDeliveryBillAddPackageScanGoodDetailJieBang params:@{@"wa_id":self.wa_id,@"g_id":self.model.g_id,@"num_sn":num_sn} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"已解除"];
            self->page = 1;
            [self requestForList];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshScanAddGoodList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ScanGoodDetailInfoCell *cell = [ScanGoodDetailInfoCell ScanGoodDetailInfoCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else {
        ScanGoodDetailListCell *cell = [ScanGoodDetailListCell ScanGoodDetailListCellWithTableView:tableView];
        ScanGoodDetailModel *model = self.dataArray[indexPath.row];
        cell.model = model;
        return cell.height;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
